#include<iostream>

//类模板:模板化的是成员变量
//template<class  T>另一种写法
template<typename T>

class DynamicArray 
{
public:
    DynamicArray();
    ~DynamicArray();
    void InsertTail(const T&element);
    DynamicArray<T>& operator=(const DynamicArray<T> &a);
  
    //友元模板的声明不能和类模板重名
    template<typename U>
    friend std::ostream& operator <<(std::ostream& os,const DynamicArray<U>&array);
private:
    T *a;
    int size;
    int capacity;

};

//类模板函数在类外定义时，需要带上模板变量定义
//类名单独出现不算完整的类型
template<typename T>

DynamicArray<T>::DynamicArray():size(0),capacity(15)
{
    a=new T[capacity];
}
template<typename T>

DynamicArray<T>::~DynamicArray()
{
    delete []a;
}

template<typename T>

void DynamicArray<T>::InsertTail(const T&element)
{
    if(size==capacity)
    {
        capacity*=2;
        T*newP=new T[capacity];
        for(int i=0;i<size;i++)
        {
            newP[i]=a[i];
        }
        delete []a;
        a=newP;
    }
    a[size++]=element;
}
template<typename T>
DynamicArray<T> &DynamicArray<T>::operator =(const DynamicArray<T> &a)
{
    delete []this->a;
    this->size=a.size;
    this->capacity=a.capacity;
    this->a=new T[this->capacity];
    for(int i=0;i<this->size;i++)
    {
        this->a[i]=a.a[i];
    }
    return *this;
}


template<typename T>
std::ostream& operator <<(std::ostream& os,const DynamicArray<T>&array)
{
    for(int i=0;i<array.size;i++)
    {
        std::cout<<array.a[i]<<" ";
    }
    std::cout<<std::endl;
    return os; 
}

